package org.jash.learnmvp.user;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import org.jash.learnmvp.R;
import org.jash.learnmvp.mvp.BaseActivity;

public class RegisterActivity extends BaseActivity<ConStruct.IRegisterView, RegisterPresenter> implements ConStruct.IRegisterView {
    private EditText username;
    private EditText password;
    private EditText phone;
    private EditText code;

    @Override
    protected int layoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initData() {
        presenter = new RegisterPresenter();
    }

    @Override
    protected void initView() {
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        phone = findViewById(R.id.phone);
        code = findViewById(R.id.code);
        findViewById(R.id.get_code).setOnClickListener(v -> presenter.loadCode(phone.getText().toString()));
        findViewById(R.id.verify).setOnClickListener(v -> presenter.verify(phone.getText().toString(), code.getText().toString()));
        findViewById(R.id.register).setOnClickListener(v -> presenter.register(username.getText().toString(), password.getText().toString(), phone.getText().toString()));
        setSupportActionBar(findViewById(R.id.toolbar));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void codeSuccess(int code) {
        this.code.setText("" + code);
    }

    @Override
    public void registerSuccess(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void verifySuccess(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void failure(Throwable throwable) {
        Toast.makeText(this, throwable.getMessage(), Toast.LENGTH_SHORT).show();
        Log.e("TAG", "failure: ", throwable);
    }
}